// using jQueryFileTree
jsForms.Templates['common.RemoteFileExplorer']='<div>'+
'<ul name="root" class="jqueryFileTree"/>'+
'</div>';

jsForms.Templates['common.RemoteFileExplorerItem']='<li><a name="name"/><ul name="sub" class="jqueryFileTree"/></li>';
jsForms.Components['common.RemoteFileExplorerItem']={
	Init:function(itemData)
	{
		this.C.name.text(itemData.n);
		this.P.name = itemData.n;
		this.P.path = itemData.p;
		this.P.type = itemData.t;
		if (itemData.t == 'd'){
			this.addClass("directory collapsed");//
		}else{
			this.addClass("file ext_"+itemData.n.replace(/^.*\./,''));
		}
		return jsForms.Components.Component.Init.call(this);
	},
	GetName:function (){
		return this.P.name;
	},
	GetPath:function(){
		return this.P.path;
	},
	GetType:function(){
		return this.P.type;
	},
	GetControlElement:function(){
		return this.C.name;
	},
	GetNameElement:function(){
		return this.C.name;
	},
	GetSubItemsContainer:function(){
		return this.C.sub;
	}
};

jsForms.Components['common.RemoteFileExplorer']={
	Init:function()
	{
		this.P.isInit = true;
		if (this.P.Filter == undefined){
			this.P.Filter = '*';
		}
		if (this.P.ItemComponentName == undefined){
			this.P.ItemComponentName = 'common.RemoteFileExplorerItem';
		}
		this.SetRoot(this.P.Root);
		return jsForms.Components.Component.Init.call(this);
	},
	GetSelectedPath:function(){
		return this.P.selectedPath;
	},
	GetSelectedItem:function(){
		return this.P.selectedItem;
	},
	ItemOnDblClick:function (e){
		e.data.parent._ExecEvent('onItemDoubleClick',e.data.item);
	},
	ItemOnClick:function (e){
		var item = e.data.item;
		var parent = e.data.parent;
		if (parent.P.selectedItem != undefined){
			parent.P.selectedItem.removeClass('selected');
		}
		parent.P.selectedItem = item;
		parent.P.selectedPath = item.GetPath();
		item.addClass('selected');
		if (item.GetType() == 'd'){
			if (item.hasClass("expanded")){
				item.removeClass("expanded").addClass('collapsed').GetSubItemsContainer().hide();
			}else{
				item.addClass('wait');
				parent.LoadSubItems(item.GetSubItemsContainer(),item.GetPath(),item);
			}
		}
	},
	LoadSubItems:function($subitem,target,$item){
		var mainControl = this;		
		function onLoad(data){
			$subitem.hide().empty();
			if (Error.isError(data)){
				console.error(Error.getErrorMessages(data));
				return;
			}
			for (var i = 0 ; i < data.length ; i++){
				var item = data[i];
				item = jsForms.CreateComponent(mainControl.P.ItemComponentName).appendTo($subitem).Init(item);
				item.GetControlElement().bind('click',{parent:mainControl,item:item},mainControl.ItemOnClick);
				item.GetNameElement().bind('dblclick',{parent:mainControl,item:item},mainControl.ItemOnDblClick);
			}
			$subitem.show();
			if ($item != undefined){
				$item.addClass("expanded").removeClass('collapsed').removeClass('wait');
			}
		}
		$.getJSON(this.P.ServerService,{action:"get_subitems",target:target,filter:this.P.Filter},onLoad);
		return this;
	},
	SetRoot:function (value){
		this.P.Root = value;
		if (this.P.isInit !=true) 
		{
			return this;
		}
		this.LoadSubItems(this.C.root,this.P.Root);
		return this;
	},
	SetItemComponentName:function(value){
		if (value == undefined){
			value = 'common.RemoteFileExplorerItem';
		}
		this.P.ItemComponentName = value;
		return this;
	}
};

// -----------------------------------------------
// Compilation INFO
// -----------------------------------------------
jsForms.CreateCompileInfoByObject(
{
	Name: 'common.RemoteFileExplorer',
	Properties: {
		Root:{
			Type: 'String',
			Access:'getset'
		},
		ServerService: {
			Type: 'String',
			Access: 'Direct'
		},
		Filter: {
			Type: 'String',
			Access: 'Direct'
		},
		ItemComponentName:{
			Type: 'String',
			Access: 'GETSET'
		}
	},
	Events: {
		onItemDoubleClick:{}
	}
});
// -----------------------------------------------
